Search Framework:
DateTime Extensions
Namespace: WealthLab.Core
Parent:

DateTime extension methods provide utility functions for working with dates and times, particularly for trading calendars, option expiration dates, date formatting, and time-based strategy logic. Call these methods directly on a DateTime instance using standard extension method syntax.

Date and Time Utilities
FirstDayOfMonth
public static DateTime FirstDayOfMonth(this DateTime dt)

Returns a DateTime representing the first day of the month containing dt.


FloorToMinute
public static DateTime FloorToMinute(this DateTime dt)

Returns a DateTime containing the same year, month, day, hour, and minute as dt, with seconds and milliseconds set to zero. The DateTime Kind is preserved.


Get24HourTime
public static int Get24HourTime(this DateTime dt)

Returns the time portion of dt as a 24-hour HHmm integer. For example, 4:00 PM returns 1600.


GetTime
public static int GetTime(this DateTime dt)

Returns the time portion of dt as a 24-hour HHmm integer. Seconds are ignored. Examples

  • 9:35 AM => 935
  • 12:00 PM => 1200
  • 3:50 PM => 1550

IsMaxValue
public static bool IsMaxValue(this DateTime dt)

Returns true if dt represents, or is very close to, DateTime.MaxValue. The method considers any DateTime whose year is 9999 to be a maximum value.


IsMinValue
public static bool IsMinValue(this DateTime dt)

Returns true if dt represents, or is very close to, DateTime.MinValue. The method considers any DateTime whose year is 2 or earlier to be a minimum value.


Quarter
public static int Quarter(this DateTime dt)

Returns the zero-based calendar quarter containing dt. Possible values are:

  • 0 = January through March
  • 1 = April through June
  • 2 = July through September
  • 3 = October through December

RemoveSeconds
public static DateTime RemoveSeconds(this DateTime dt)

Returns a DateTime containing the same year, month, day, hour, and minute as dt, with the seconds set to zero.


SemiAnnual
public static int SemiAnnual(this DateTime dt)

Returns the zero-based half-year containing dt. Returns 0 for January through June and 1 for July through December.



Date Differences
CalendarDaysBetweenDates
public static int CalendarDaysBetweenDates(this DateTime fromDate, DateTime toDate)
public static int CalendarDaysBetweenDates(this int fromDate, int toDate)
public static int CalendarDaysBetweenDates(this string fromDate, string toDate)

Returns the number of calendar days between fromDate and toDate. The result is positive when toDate is later, zero when the dates are the same, and negative when toDate is earlier. When using the string overload, dates must be in yyyyMMdd format, for example:

int days = "20260801".CalendarDaysBetweenDates("20260826");

When using the integer overload, dates must also be expressed in yyyyMMdd format:

int days = 20260801.CalendarDaysBetweenDates(20260826);


Formatting
JSFormat
public static string JSFormat(this DateTime dt)

Returns dt formatted as a JavaScript-oriented date/time string using the form yyyy-MM-ddThh:mm:ssZ.


ToISO8601
public static string ToISO8601(this DateTime dateTime)

Converts dateTime to UTC and returns an ISO 8601-style date/time string.


ToNewDateString
public static string ToNewDateString(this DateTime dt)

Returns C# source code representing the date portion of dt as a DateTime constructor. For example, a DateTime representing August 26, 2026 returns:

new DateTime(2026,8,26)

ToShortDateTimeString
public static string ToShortDateTimeString(this DateTime dt, bool useSeconds = false)

Returns dt using the current culture's short date format followed by a 24-hour time. By default, the time contains hours and minutes. Pass true for useSeconds to include seconds.


ToSmartString
public static string ToSmartString(this DateTime dt)

Returns a short date and time string if dt contains a non-zero time component. If the time is midnight, only the short date is returned.



Options Calendar
IsOptionExpiry
public static bool IsOptionExpiry(this DateTime date)
public static bool IsOptionExpiry(this DateTime date, BarHistory bars)

Determines whether date is a monthly option expiration date. The parameterless version identifies the third Friday of the month and also accounts for U.S. stock market holidays that move expiration from Friday to Thursday. The BarHistory version uses the market calendar associated with bars to determine the applicable expiration date.


NextOptionExpiryDate
public static DateTime NextOptionExpiryDate(this DateTime date, BarHistory bars, bool useWeeklies = false)

Returns the next option expiration date on or after date. By default, the method returns the next monthly expiration, normally the third Friday of the month. If the expiration Friday is a market holiday, the preceding trading day is returned. Pass true for useWeeklies to determine the next weekly expiration instead.


NthDayOfWeek
public static DateTime NthDayOfWeek(this DateTime dt, int nth, DayOfWeek dow)

Returns the nth occurrence of the specified dow DayOfWeek within the month containing dt. For example, the following returns the third Friday of the month:

DateTime thirdFriday = dt.NthDayOfWeek(3, DayOfWeek.Friday);

OptionsWeekOfMonth
public static int OptionsWeekOfMonth(this DateTime date)

Returns the options week of the month, from 1 through 5. Options weeks are defined as Saturday through Friday, and a week belongs to the month containing its Friday.


OptionsWeekOfYear
public static int OptionsWeekOfYear(this DateTime date)

Returns the options week of the year, from 1 through 53. Options weeks are defined as Saturday through Friday and belong to the year containing the week's Friday.


ThirdFriday
public static DateTime ThirdFriday(DateTime tempDate)

Returns the date of the third Friday in the month containing tempDate.



Trading Calendar
GetNextTradingDate
public static DateTime GetNextTradingDate(this DateTime dt, BarHistory bars)
public static DateTime GetNextTradingDate(this DateTime dt, MarketDetails mkt)

Returns the next trading date after dt, based on the market associated with bars, or the specified mkt. Market trading days and holidays are taken into account. If dt contains a time component, it is preserved in the returned value.


IsATradingDay
public static bool IsATradingDay(this DateTime dt, BarHistory bars)

Returns true if the market associated with bars trades on the date represented by dt. Holidays and the market's configured trading days of the week are taken into account. The time portion of dt is ignored.


IsHoliday
public static bool IsHoliday(this DateTime dt, BarHistory bars)

Returns true if the date represented by dt is contained in the holiday dates of the market associated with bars.


IsLastTradingDayOfMonth
public static bool IsLastTradingDayOfMonth(this DateTime dt, BarHistory bars)

Returns true if dt represents the last trading day of a calendar month for the market associated with bars.


IsLastTradingDayOfWeek
public static bool IsLastTradingDayOfWeek(this DateTime dt, BarHistory bars)

Returns true if dt represents the last trading day of a calendar week for the market associated with bars.


IsTradingDay
public static bool IsTradingDay(this DateTime dt, BarHistory bars)

Returns true if the market associated with bars trades on the date represented by dt. Holidays and the market's configured trading days of the week are taken into account. The time portion of dt is ignored.


IsWeekend
public static bool IsWeekend(this DateTime dt)

Returns true if dt falls on a Saturday or Sunday.


PreviousTradingDay
public static DateTime PreviousTradingDay(this DateTime dt, MarketDetails md)

Returns the trading day immediately preceding dt, based on the specified md MarketDetails instance. If dt contains a time component, it is preserved.


TradingDayOfMonth
public static int TradingDayOfMonth(this DateTime dt, BarHistory bars)

Returns the trading-day number within the month for dt, based on the market associated with bars.


TradingHourOfDay
public static int TradingHourOfDay(this DateTime dt, BarHistory bars)

Returns the trading-hour number for dt based on the market hours associated with bars. Returns zero when dt is outside regular market hours.



Unix Time
ToUnixDateTime
public static long ToUnixDateTime(this DateTime dt)

Returns the number of milliseconds between January 1, 1970 and dt.


UnixDateToDateTime
public static DateTime UnixDateToDateTime(this long unixDate, bool inputInMS = false)

Converts a Unix timestamp to a UTC DateTime. By default, unixDate is interpreted as seconds since the Unix epoch. Pass true for inputInMS if the value is already expressed in milliseconds.



Week and Period Utilities
IsBiweeklyStart
public static bool IsBiweeklyStart(this DateTime dt)

Returns true if dt falls in the first week of the class's alternating two-week cycle. The cycle is anchored to January 1, 1900. The supplied date is assumed to represent the first trading day of a week, allowing a Tuesday following a Monday holiday to be treated as belonging to that Monday's week.


WeekOfYear
public static int WeekOfYear(this DateTime dte)

Returns the calendar week number of dte, from 1 through 52 or 53. Week 1 is defined as the first full Sunday-through-Saturday week in January. Consequently, dates at the beginning of January can belong to the final week of the preceding year's week sequence.